/* eslint valid-jsdoc: "off" */

'use strict';
const Path = require('path');
// 所有授权接口
const adminApis = require('./auth/adminApis');
// IP 锁配置文件
const ipLookConfig = require('./ipLook');
/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = {
    // 配置模板引擎
    view: {
      defaultViewEngine: 'nunjucks',
      defaultExtension: '.njk',
      mapping: {
        '.njk': 'nunjucks',
      },
    },
    ybproxy: {
      url: 'http://localhost:4000',
    },
    cluster: {
      listen: {
        port: 3110,
      },
    },
    security: {
      csrf: {
        // enable: false,
        queryName: '_csrf', // 通过 query 传递 CSRF token 的默认字段为 _csrf
        bodyName: '_csrf', // 通过 body 传递 CSRF token 的默认字段为 _csrf
      },
    },
    session: {
      key: 'YB_NUXT_EGG_SESSION',
      maxAge: 2 * 3600 * 1000,
      httpOnly: true,
      encrypt: true,
      renew: true,
    },
    bodyParser: {
      jsonLimit: '100kb',
      formLimit: '2mb',
    },
    multipart: {
      fileSize: '2mb',
      mode: 'stream',
      fileModeMatch: /^\/api\/server\/image-up$/,
      tmpdir: Path.resolve(__dirname, '../app/public/up-image/'),
    },
    cors: {
      origin: '*',
      allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
    },
    baiDu: {
      APP_ID: '10672586',
      APP_KEY: '7ksfGnsk6U46gtXeML31wmwC',
      SECRET_KEY: 'WDUHXun4W3gGXfPnIm0M0irW46KAl5Hr',
    },
    adminApis,
    ali: {
      admin: {
        accessKeyId: 'LTAIFCwByU3QR6qX',
        accessKeySecret: 'ECEB3D503GLUpAA5xh8IzAuKd39vuw'
      },
      accessKeyId: 'LTAI4G13c1oDjzU6jMybPwBe',
      region: 'oss-cn-beijing',
      accessKeySecret: 'PPcHP8zFR82Rj5T9tHePcRVABITO6f',
      cdn: 'https://cdn.wangyabin.top',
      ossRole: 'acs:ram::1116311948072463:role/wang-oss',
      TokenExpireTime: '3600',
      bucket: 'ybweb'
    },
    // auth中间件的配置
    auth: {
      match: '/admin',
    },
    elasticsearch: {
      host: 'localhost:9200',
      apiVersion: '7.x'
    }
  };

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_415461850';

  // 引用部署在反向代理中
  config.proxy = true;

  // 配置中间件
  config.middleware = [
    'errorHandler',
    'ipLook',
    'auth',
  ];

  config.io = {
    namespace: {
      '/chat': {
        connectionMiddleware: [ 'chatConnection' ],
        packetMiddleware: [ 'cactPacket' ],
      },
    },
    // cluster 模式下，通过 redis 实现数据共享
    redis: {
      host: '127.0.0.1',
      port: 6379,
    },
  };

  config.redis = {
    client: {
      port: 6379,
      host: '127.0.0.1',
      password: '',
      db: 0,
    },
  };

  // OAuth Github
  config.OAuth = {
    qq: {
      OpenIDUrl: 'https://graph.qq.com/oauth2.0/me',
      appid: '101808196',
      userInfoUrl: 'https://graph.qq.com/user/get_user_info',
    },
    GitHub: {
      key: '726e3a78a8f4498d043e',
      secret: 'e775257e5caa5da88bdab8e7779f78f931c53368',
    },
  };

  // 短连接
  config.shortConnection = {
    // 失效时间 1天
    overdueTime: 3600 * 24,
    // 链接地址
    link: 'http://localhost/webnuxt-api/short-connection',
  };

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  // IP锁定
  config.fullIP = ipLookConfig;

  return {
    ...config,
    ...userConfig,
  };
};
